﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PRO_DOCS.BLL.WIN.Authentication
{
    public static class LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY
    {
        public static Dictionary<long, DAL.Database.ARCHIVE_DOCUMENT_TYPE> CURRENT_LOGGED_IN_USER_ALLOWED_DOCUMENT_TYPES
        { get; set; }

        private static Dictionary<long, DAL.Database.ARCHIVE_DEPARTMENT> CURRENT_LOGGED_IN_USER_DENIED_ARCHIVE_DEPARTMENTS
        { get; set; }

        public static Dictionary<long, DAL.Database.ARCHIVE_DEPARTMENT> CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS
        { get; set; }

        public static Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> CURRENT_LOGGED_IN_USER_ALLOWED_FOLDERS
        { get; set; }

        public static Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> CURRENT_LOGGED_IN_USER_DENIED_FOLDERS
        { get; set; }

        private static Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> LOGGED_IN_ROLES_ALLOWED_FOLDERS
        { get; set; }

        private static Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> LOGGED_IN_USERS_ALLOWED_FOLDERS
        { get; set; }

        public static string CURRENT_LOGGED_IN_USER_ALLOWED_SEARCH_FOLDERS
        { get; set; }


        public static void LOAD_LOGGED_IN_ARCHIVES_SECURITY_SETTINGS()
        {
            CURRENT_LOGGED_IN_USER_ALLOWED_SEARCH_FOLDERS = string.Empty;

            CURRENT_LOGGED_IN_USER_DENIED_ARCHIVE_DEPARTMENTS = new Dictionary<long, DAL.Database.ARCHIVE_DEPARTMENT>();
            CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS = new Dictionary<long, DAL.Database.ARCHIVE_DEPARTMENT>();
            CURRENT_LOGGED_IN_USER_ALLOWED_DOCUMENT_TYPES = new Dictionary<long, DAL.Database.ARCHIVE_DOCUMENT_TYPE>();

            CURRENT_LOGGED_IN_USER_ALLOWED_FOLDERS = new Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE>();
            CURRENT_LOGGED_IN_USER_DENIED_FOLDERS = new Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE>();

            LOGGED_IN_ROLES_ALLOWED_FOLDERS = new Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE>();
            LOGGED_IN_USERS_ALLOWED_FOLDERS = new Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE>();

            //1. get the denied user archive department
            CURRENT_LOGGED_IN_USER_DENIED_ARCHIVE_DEPARTMENTS = LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.ARCHIVE_DEPARTMENT_DENIED_LOGIN_USERs.ToDictionary(k => k.ARCHIVE_DEPARTMENT_ID, v => v.ARCHIVE_DEPARTMENT);
            //2. get the allowed user archive departments
            Dictionary<long, DAL.Database.ARCHIVE_DEPARTMENT> _aLLOWED_USER_DEPARTMENTS = LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.ARCHIVE_DEPARTMENT_LOGIN_USERs.ToDictionary(k => k.ARCHIVE_DEPARTMENT_ID, v => v.ARCHIVE_DEPARTMENT);
            foreach (KeyValuePair<long, DAL.Database.ARCHIVE_DEPARTMENT> _aLLOWED_USER_DEPARTMENT in _aLLOWED_USER_DEPARTMENTS)
            {
                //if this department is not denied for the user 
                if (!CURRENT_LOGGED_IN_USER_DENIED_ARCHIVE_DEPARTMENTS.ContainsKey(_aLLOWED_USER_DEPARTMENT.Key))
                {
                    if (!_aLLOWED_USER_DEPARTMENT.Value.IS_DELETED.Value)
                    {
                        CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS.Add(_aLLOWED_USER_DEPARTMENT.Key, _aLLOWED_USER_DEPARTMENT.Value);
                    }
                }
            }


            //3. get the allowed role Archive Departments
            foreach (KeyValuePair<long, DAL.Database.SYSTEM_ROLE> _lOGGED_IN_USER_ROLE in LOGGED_IN_EMPLOYEE_DETAILS.CURRENT_LOGGED_IN_USER_ROLES)
            {
                Dictionary<long, DAL.Database.ARCHIVE_DEPARTMENT> _aLLOWED_ROLE_DEPARTMENTS = _lOGGED_IN_USER_ROLE.Value.ARCHIVE_DEPARTMENT_ROLEs.ToDictionary(k => k.ARCHIVE_DEPARTMENT_ID, v => v.ARCHIVE_DEPARTMENT);
                foreach (KeyValuePair<long, DAL.Database.ARCHIVE_DEPARTMENT> _aLLOWED_ROLE_DEPARTMENT in _aLLOWED_ROLE_DEPARTMENTS)
                {
                    // if not the archive is already added
                    if (!CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS.ContainsKey(_aLLOWED_ROLE_DEPARTMENT.Key))
                    {
                        //if this department is not denied for the user 
                        if (!CURRENT_LOGGED_IN_USER_DENIED_ARCHIVE_DEPARTMENTS.ContainsKey(_aLLOWED_ROLE_DEPARTMENT.Key))
                        {
                            if (!_aLLOWED_ROLE_DEPARTMENT.Value.IS_DELETED.Value)
                            {
                                CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS.Add(_aLLOWED_ROLE_DEPARTMENT.Key, _aLLOWED_ROLE_DEPARTMENT.Value);
                            }
                        }
                    }
                }

                //4. get the denied roles folders
                {
                    Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _dENIED_ROLE_FOLDERS = _lOGGED_IN_USER_ROLE.Value.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_ROLEs.ToDictionary(k => k.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID, v => v.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                    foreach (KeyValuePair<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _dENIED_ROLE_FOLDER in _dENIED_ROLE_FOLDERS)
                    {
                        if (!CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.ContainsKey(_dENIED_ROLE_FOLDER.Key))
                        {
                            CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.Add(_dENIED_ROLE_FOLDER.Key, _dENIED_ROLE_FOLDER.Value);
                        }
                    }
                }
                //5. get the allowed roles folders
                {
                    Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _aLLOWED_ROLE_FOLDERS = _lOGGED_IN_USER_ROLE.Value.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ROLEs.ToDictionary(k => k.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID, v => v.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                    foreach (KeyValuePair<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _aLLOWED_ROLE_FOLDER in _aLLOWED_ROLE_FOLDERS)
                    {
                        if (!CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.ContainsKey(_aLLOWED_ROLE_FOLDER.Key))
                        {
                            if (!LOGGED_IN_ROLES_ALLOWED_FOLDERS.ContainsKey(_aLLOWED_ROLE_FOLDER.Key))
                            {
                                LOGGED_IN_ROLES_ALLOWED_FOLDERS.Add(_aLLOWED_ROLE_FOLDER.Key, _aLLOWED_ROLE_FOLDER.Value);
                            }
                        }
                    }
                }
            }
            //4. get the denied users folders
            Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _dENIED_USER_FOLDERS = LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_DENIED_LOGIN_USERs.ToDictionary(k => k.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID, v => v.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
            foreach (KeyValuePair<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _dENIED_USER_FOLDER in _dENIED_USER_FOLDERS)
            {
                if (!CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.ContainsKey(_dENIED_USER_FOLDER.Key))
                {
                    CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.Add(_dENIED_USER_FOLDER.Key, _dENIED_USER_FOLDER.Value);
                }
            }

            //5. get the allowed user folders
            {
                Dictionary<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _aLLOWED_USERS_FOLDERS = LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_LOGIN_USERs.ToDictionary(k => k.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID, v => v.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                foreach (KeyValuePair<Guid, DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> _aLLOWED_USER_FOLDER in _aLLOWED_USERS_FOLDERS)
                {
                    if (!CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.ContainsKey(_aLLOWED_USER_FOLDER.Key))
                    {
                        if (!LOGGED_IN_USERS_ALLOWED_FOLDERS.ContainsKey(_aLLOWED_USER_FOLDER.Key))
                        {
                            LOGGED_IN_USERS_ALLOWED_FOLDERS.Add(_aLLOWED_USER_FOLDER.Key, _aLLOWED_USER_FOLDER.Value);
                        }
                    }
                }
            }

            LOAD_ARCHIVE_DEPARTMENTS_ONLY_PERMITTED_FOLDER_STRUCTURE();

            foreach (KeyValuePair<long, DAL.Database.ARCHIVE_DEPARTMENT> _aLLOWED_USER_DEPARTMENT in CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS)
            {
                //get the allowed document Types only
                List<DAL.Database.ARCHIVE_DEPARTMENT_DOCUMENT_TYPE> _dEP_DOCUMENT_TYPES = _aLLOWED_USER_DEPARTMENT.Value.ARCHIVE_DEPARTMENT_DOCUMENT_TYPEs.ToList();

                for (int i = 0; i < _dEP_DOCUMENT_TYPES.Count; i++)
                {
                    if (!CURRENT_LOGGED_IN_USER_ALLOWED_DOCUMENT_TYPES.ContainsKey(_dEP_DOCUMENT_TYPES[i].ARCHIVE_DEPARTMENT_DOCUMNET_TYPE_ID))
                    {
                        CURRENT_LOGGED_IN_USER_ALLOWED_DOCUMENT_TYPES.Add(_dEP_DOCUMENT_TYPES[i].ARCHIVE_DEPARTMENT_DOCUMNET_TYPE_ID, _dEP_DOCUMENT_TYPES[i].ARCHIVE_DOCUMENT_TYPE);
                    }
                }
            }
        }

        private static void LOAD_ARCHIVE_DEPARTMENTS_ONLY_PERMITTED_FOLDER_STRUCTURE()
        {
            List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT> aRCHIVE_DEPARTMENTS = CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS.Values.ToList();
            for (int i = 0; i < aRCHIVE_DEPARTMENTS.Count; i++)
            {
                BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE(aRCHIVE_DEPARTMENTS[i]);
            }
        }

        private static void BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE)
        {

            List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTUREs.ToList();

            for (int i = 0; i < aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES.Count; i++)
            {
                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES[i];

                //a. check if it don't have permitted roles or bermitted users
                //b. check if it is in the denied folders list
                //c. check the visibility
                bool continueCheck = false;
                bool isAcceptedFolder = false;
                bool hasAllowedRoles = _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ROLEs.ToList().Count > 0;
                bool hasAllowedUsers = _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_LOGIN_USERs.ToList().Count > 0;

                #region a. check if it don't have permitted roles or bermitted users
                if (hasAllowedRoles || hasAllowedUsers)
                {
                    continueCheck = LOGGED_IN_USERS_ALLOWED_FOLDERS.ContainsKey(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID);
                }
                else
                {
                    continueCheck = true;
                }
                #endregion

                #region b. check if it is in the denied folders list
                if (continueCheck)
                {
                    continueCheck = !CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.ContainsKey(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID);
                }
                #endregion

                #region c. check the visibility
                if (continueCheck)
                {
                    isAcceptedFolder = !_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.IS_DELETED.Value;
                }
                #endregion

                //BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                if (isAcceptedFolder)
                {
                    //1. add to the permitted folders
                    CURRENT_LOGGED_IN_USER_ALLOWED_FOLDERS.Add(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID, _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                    //2. add the string for the search
                    ADD_THE_SEARCH_STRING(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID);
                    //3. continue looping
                    BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                }
            }
        }

        private static void ADD_THE_SEARCH_STRING(Guid _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID)
        {
            string comma = (CURRENT_LOGGED_IN_USER_ALLOWED_SEARCH_FOLDERS.Length > 0) ? ", " : string.Empty;

            CURRENT_LOGGED_IN_USER_ALLOWED_SEARCH_FOLDERS += string.Format("{0}'{1}'", comma, _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID.ToString());
        }

        public static void BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE(PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT aRCHIVE_DEPARTMENT)
        {
            {
                //Gets the 1st Level
                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES = aRCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTUREs.ToList();


                for (int i = 0; i < aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES.Count; i++)
                {
                    PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES[i];

                    if (aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.PARENT_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID.Value.Equals(Guid.Empty))
                    {
                        //a. check if it don't have permitted roles or bermitted users
                        //b. check if it is in the denied folders list
                        //c. check the visibility
                        bool continueCheck = false;
                        bool isAcceptedFolder = false;
                        bool hasAllowedRoles = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ROLEs.ToList().Count > 0;
                        bool hasAllowedUsers = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_LOGIN_USERs.ToList().Count > 0;

                        #region a. check if it don't have permitted roles or bermitted users
                        if (hasAllowedRoles || hasAllowedUsers)
                        {
                            continueCheck = LOGGED_IN_USERS_ALLOWED_FOLDERS.ContainsKey(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID);
                        }
                        else
                        {
                            continueCheck = true;
                        }
                        #endregion

                        #region b. check if it is in the denied folders list
                        if (continueCheck)
                        {
                            continueCheck = !CURRENT_LOGGED_IN_USER_DENIED_FOLDERS.ContainsKey(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID);
                        }
                        #endregion

                        #region c. check the visibility
                        if (continueCheck)
                        {
                            isAcceptedFolder = !aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.IS_DELETED.Value;
                        }
                        #endregion

                        if (isAcceptedFolder)
                        {
                            //1. add to the permitted folders
                            CURRENT_LOGGED_IN_USER_ALLOWED_FOLDERS.Add(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID, aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                            //2. add the string for the search
                            ADD_THE_SEARCH_STRING(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID);
                            //3. continue looping
                            BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                        }
                    }
                }

            }
        }
    }
}
